* Table A3: Sensitivity of Rank-Rank Estimates to Minimum Income Thresholds

clear
use "${data_dir}DavisMazumderData.dta"

keep if age_firstSurvey<=18
drop if miss0_old | miss1_old
drop if (mom_outOfRange==1 & linkMom==1 & !(dad_outOfRange==0 & linkDad==1)) | (dad_outOfRange==1 & linkDad==1 & !(mom_outOfRange==0 & linkMom==1)) | (linkMom==0 & linkDad==0) 
 




* ---------------- *
* Ranks - Old Base *
* ---------------- *


gen N = .
 tab n  if surv79==0 & women==0 & faminc1_oldbase<. & faminc0_oldbase<. [w=weight]
 replace N = r(N) if surv79==0 & women==0
 tab n  if surv79==0 & women==1 & faminc1_oldbase<. & faminc0_oldbase<. [w=weight]
 replace N = r(N) if surv79==0 & women==1
 tab n  if surv79==1 & women==0 & faminc1_oldbase<. & faminc0_oldbase<. [w=weight] 
 replace N = r(N) if surv79==1 & women==0
 tab n  if surv79==1 & women==1 & faminc1_oldbase<. & faminc0_oldbase<. [w=weight] 
 replace N = r(N) if surv79==1 & women==1

set sortseed 6688571
sort women surv79 faminc0_oldbase
by women surv79: gen __rank0 = sum(weight) 
gen _rank0 = __rank0/N 
bys women surv79 faminc0_oldbase: egen rank0 = mean(_rank0) 

set sortseed 6688571 
sort women surv79 faminc1_oldbase
by women surv79: gen __rank1 = sum(weight) 
gen _rank1 = __rank1/N 
bys women surv79 faminc1_oldbase: egen rank1 = mean(_rank1) 

gen perc66 = rank0*(1-surv79)
gen perc79 = rank0*surv79

drop N __rank0 _rank0 __rank1 _rank1



reg rank1 perc66 perc79 surv79 women [w=weight], cluster(hhid)
eststo rank_oldbase
test perc66=perc79
estadd scalar p = r(p): rank_oldbase
gen s=e(sample)
count if surv79==0 & s==1
estadd scalar N66 = r(N): rank_oldbase
count if surv79==1 & s==1
estadd scalar N79 = r(N): rank_oldbase
drop s
lincom perc79-perc66
estadd scalar diff = r(estimate): rank_oldbase
estadd scalar se = r(se): rank_oldbase
global rank_main_est = r(estimate)
global rank_main_se = r(se)
	
reg rank1 perc66 perc79 surv79  [w=weight] if women==1, cluster(hhid)
eststo rank_oldbase_w
test perc66=perc79
estadd scalar p = r(p): rank_oldbase_w
gen s=e(sample)
count if surv79==0 & s==1
estadd scalar N66 = r(N): rank_oldbase_w
count if surv79==1 & s==1
estadd scalar N79 = r(N): rank_oldbase_w
drop s
lincom perc79-perc66
estadd scalar diff = r(estimate): rank_oldbase_w
estadd scalar se = r(se): rank_oldbase_w
global rank_main_est = r(estimate)
global rank_main_se = r(se)
	
reg rank1 perc66 perc79 surv79  [w=weight] if women==0, cluster(hhid)
eststo rank_oldbase_m
test perc66=perc79
estadd scalar p = r(p): rank_oldbase_m
gen s=e(sample)
count if surv79==0 & s==1
estadd scalar N66 = r(N): rank_oldbase_m
count if surv79==1 & s==1
estadd scalar N79 = r(N): rank_oldbase_m
drop s
lincom perc79-perc66
estadd scalar diff = r(estimate): rank_oldbase_m
estadd scalar se = r(se): rank_oldbase_m
global rank_main_est = r(estimate)
global rank_main_se = r(se)


drop nkid_* nparent_*

foreach min in 0 1000 1500 2500 {
	
	
	drop rank1 rank0 perc66 perc79
	
	* ---------------- *
	* Ranks - Above Min *
	* ---------------- *


	gen N = .
		tab n  if surv79==0 & women==0 & tot0_min`min'<. & tot1_min`min'<. [w=weight]
		replace N = r(N) if surv79==0 & women==0
		tab n  if surv79==0 & women==1  & tot0_min`min'<. & tot1_min`min'<. [w=weight]
		replace N = r(N) if surv79==0 & women==1
		tab n  if surv79==1 & women==0  & tot0_min`min'<. & tot1_min`min'<. [w=weight]
		replace N = r(N) if surv79==1 & women==0
		tab n  if surv79==1 & women==1  & tot0_min`min'<. & tot1_min`min'<. [w=weight]
		replace N = r(N) if surv79==1 & women==1
	
	set sortseed 6688571
	sort women surv79 tot0_min`min'
	by women surv79: gen __rank0 = sum(weight) if tot0_min`min'<. & tot1_min`min'<.
	gen _rank0 = __rank0/N if tot0_min`min'<. & tot1_min`min'<.
	bys women surv79 tot0_min`min': egen rank0 = mean(_rank0)  if tot0_min`min'<. & tot1_min`min'<.
	
	set sortseed 6688571
	sort women surv79 tot1_min`min'
	by women surv79: gen __rank1 = sum(weight) if tot0_min`min'<. & tot1_min`min'<.
	gen _rank1 = __rank1/N if tot0_min`min'<. & tot1_min`min'<.
	bys women surv79 tot1_min`min': egen rank1 = mean(_rank1) if tot0_min`min'<. & tot1_min`min'<.

	gen perc66 = rank0*(1-surv79)
	gen perc79 = rank0*surv79

	drop N __rank0 _rank0 __rank1 _rank1
	
	reg rank1 perc66 perc79 surv79 women [w=weight], cluster(hhid)
	eststo rank_`min'
	test perc66=perc79
	estadd scalar p = r(p): rank_`min'
	gen s=e(sample)
	count if surv79==0 & s==1
	estadd scalar N66 = r(N): rank_`min'
	count if surv79==1 & s==1
	estadd scalar N79 = r(N): rank_`min'
	drop s
	lincom perc79-perc66
	estadd scalar diff = r(estimate): rank_`min'
	estadd scalar se = r(se): rank_`min'
	global rank_main_est = r(estimate)
	global rank_main_se = r(se)
	
	reg rank1 perc66 perc79 surv79 women [w=weight] if women==1, cluster(hhid)
	eststo rank_`min'_w
	test perc66=perc79
	estadd scalar p = r(p): rank_`min'_w
	gen s=e(sample)
	count if surv79==0 & s==1
	estadd scalar N66 = r(N): rank_`min'_w
	count if surv79==1 & s==1
	estadd scalar N79 = r(N): rank_`min'_w
	drop s
	lincom perc79-perc66
	estadd scalar diff = r(estimate): rank_`min'_w
	estadd scalar se = r(se): rank_`min'_w
	global rank_main_est = r(estimate)
	global rank_main_se = r(se)
	
	reg rank1 perc66 perc79 surv79 women [w=weight] if women==0, cluster(hhid)
	eststo rank_`min'_m
	test perc66=perc79
	estadd scalar p = r(p): rank_`min'_m
	gen s=e(sample)
	count if surv79==0 & s==1
	estadd scalar N66 = r(N): rank_`min'_m
	count if surv79==1 & s==1
	estadd scalar N79 = r(N): rank_`min'_m
	drop s
	lincom perc79-perc66
	estadd scalar diff = r(estimate): rank_`min'_m
	estadd scalar se = r(se): rank_`min'_m
	global rank_main_est = r(estimate)
	global rank_main_se = r(se)
	
}

#delimit ;
estout rank_oldbase rank_0 rank_1000 rank_1500 rank_2500
	using  "${results_dir}tableA3.txt", replace
	keep(perc66 perc79) 
	cells(b(star fmt(3))  se(par fmt(3))) 
	collabels(,none) stat(p N66 N79 diff se) stardrop(*) ;	
	
estout rank_oldbase_w rank_0_w rank_1000_w rank_1500_w rank_2500_w
	using  "${results_dir}tableA3.txt", append
	keep(perc66 perc79)
	cells(b(star fmt(3))  se(par fmt(3))) 
	collabels(,none) stat(p N66 N79 diff se) stardrop(*) ;	
	
estout rank_oldbase_m rank_0_m rank_1000_m rank_1500_m rank_2500_m
	using  "${results_dir}tableA3.txt", append
	keep(perc66 perc79)
	cells(b(star fmt(3))  se(par fmt(3))) 
	collabels(,none) stat(p N66 N79 diff se) stardrop(*) ;	
	
#delimit cr	
